package com.artPreter.service.impl;


import com.artPreter.mapper.ArtworkMapper;
import com.artPreter.mapper.IndexItemRelativeMapper;
import com.artPreter.pojo.Article;
import com.artPreter.pojo.Artwork;
import com.artPreter.pojo.EventExhibition;
import com.artPreter.pojo.IndexItemRelative;
import com.artPreter.service.IIndexItemRelativeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页板块-内容关系表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-12-10
 */
@Service
public class IndexItemRelativeServiceImpl extends ServiceImpl<IndexItemRelativeMapper, IndexItemRelative> implements IIndexItemRelativeService {

    @Autowired
    private ArtworkMapper artworkMapper;

    @Override
    public IPage<IndexItemRelative> getPage(Integer currentPage, Integer size, IndexItemRelative indexItemRelative) {
        return null;
    }

    @Override
    public IPage<Artwork> getNoRelativeArtworkPage(Integer currentPage, Integer size, IndexItemRelative indexItemRelative) {
        IPage<Artwork> artworkIPage = new Page<>(currentPage, size);
        if(indexItemRelative.getIndexItemId() == null|| indexItemRelative.getIndexItemId().equals("")){
            return artworkIPage;
        }
        LambdaQueryWrapper<Artwork> artworkQueryWrapper = new LambdaQueryWrapper<>();
        Boolean flag = false;
        switch (indexItemRelative.getIndexItemId()){
            // 首页作品在线
            case "830388e7-538a-45f5-9201-cf2c67117674":
                // 作品在线
                artworkQueryWrapper.eq(Artwork::getType,1);
                // 名称
                if(indexItemRelative.getName()!=null && !indexItemRelative.getName().equals("")){
                    artworkQueryWrapper.like(Artwork::getName,indexItemRelative.getName());
                }
                // 有效艺术品
                artworkQueryWrapper.eq(Artwork::getStatus,1);
                // 未关联
                artworkQueryWrapper.notExists("SELECT 1 FROM jv_index_item_relative WHERE jv_index_item_relative.type =3 and = jv_index_item_relative.index_item_id = '"+indexItemRelative.getIndexItemId()+"' and jv_index_item_relative.content_id = jv_artwork.artwork_id");
                flag = true;
                break;
        }
        if(flag){
            artworkIPage = artworkMapper.selectPage(artworkIPage,artworkQueryWrapper);
        }
        return artworkIPage;
    }

    @Override
    public IPage<Artwork> getIndexRelativeArtworkPage(Integer currentPage, Integer size, IndexItemRelative indexItemRelative) {
        IPage<Artwork> artworkIPage = new Page<>(currentPage, size);
        if(indexItemRelative.getIndexItemId() == null|| indexItemRelative.getIndexItemId().equals("")){
            return artworkIPage;
        }
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        // 作品在线
        artworkQueryWrapper.eq("b.type",1);
        // 名称
        if(indexItemRelative.getName()!=null && !indexItemRelative.getName().equals("")){
            artworkQueryWrapper.like("b.name",indexItemRelative.getName());
        }
        // 有效艺术品
        artworkQueryWrapper.eq("s.index_item_id",indexItemRelative.getIndexItemId());
        artworkQueryWrapper.eq("b.status",1);
        artworkQueryWrapper.orderByDesc("s.sort");
        artworkIPage = artworkMapper.getIndexRelativeArtworkPage(artworkIPage,artworkQueryWrapper);
        return artworkIPage;
    }

    @Override
    public void saveSort(List<IndexItemRelative> indexItemRelativeList) {
        for (int i = 0; i < indexItemRelativeList.size(); i++) {
            if(indexItemRelativeList.get(i).getContentId() != null&&indexItemRelativeList.get(i).getIndexItemId()!= null&&!indexItemRelativeList.get(i).getIndexItemId().equals("")&&!indexItemRelativeList.get(i).getContentId().equals("")){
                LambdaQueryWrapper<IndexItemRelative> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(IndexItemRelative::getIndexItemId,indexItemRelativeList.get(i).getIndexItemId());
                lambdaQueryWrapper.eq(IndexItemRelative::getContentId,indexItemRelativeList.get(i).getContentId());
                this.baseMapper.update(indexItemRelativeList.get(i),lambdaQueryWrapper);
            }
        }
    }


    @Override
    public List<IndexItemRelative> getList(IndexItemRelative indexItemRelative) {
        return null;
    }

    @Override
    public void delete(IndexItemRelative indexItemRelative) {
        System.out.println(indexItemRelative.getContentId());
        System.out.println(indexItemRelative.getIndexItemId());
        this.baseMapper.deleteById(indexItemRelative);
        if(indexItemRelative.getContentId() != null&&indexItemRelative.getIndexItemId()!= null&&!indexItemRelative.getIndexItemId().equals("")&&!indexItemRelative.getContentId().equals("")){
            LambdaQueryWrapper<IndexItemRelative> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(IndexItemRelative::getIndexItemId,indexItemRelative.getIndexItemId());
            lambdaQueryWrapper.eq(IndexItemRelative::getContentId,indexItemRelative.getContentId());
            this.baseMapper.delete(lambdaQueryWrapper);
        }
    }

    @Override
    public void edit(IndexItemRelative indexItemRelative) {
        this.baseMapper.updateById(indexItemRelative);
    }

    @Override
    public void add(IndexItemRelative indexItemRelative) {
        LambdaQueryWrapper<IndexItemRelative> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(indexItemRelative.getIndexItemId()!=null&&!indexItemRelative.getIndexItemId().equals("")){
            lambdaQueryWrapper.eq(IndexItemRelative::getIndexItemId,indexItemRelative.getIndexItemId());
        }
        if(indexItemRelative.getContentId()!=null&&!indexItemRelative.getContentId().equals("")){
            lambdaQueryWrapper.eq(IndexItemRelative::getContentId,indexItemRelative.getContentId());
        }
        List<IndexItemRelative> list = this.baseMapper.selectList(lambdaQueryWrapper);
        if(list.size()>0){
            return;
        }else{
            this.baseMapper.insert(indexItemRelative);
        }
    }

    @Override
    public IPage<EventExhibition> getNoRelativeEventExhibitionPage(Integer currentPage, Integer size, IndexItemRelative indexItemRelative) {
        IPage<EventExhibition> eventExhibitionIPage = new Page<>(currentPage, size);
        if(indexItemRelative.getIndexItemId() == null|| indexItemRelative.getIndexItemId().equals("")){
            return eventExhibitionIPage;
        }
        QueryWrapper<EventExhibition> eventExhibitionQueryWrapper = new QueryWrapper<>();
        // 名称
        if(indexItemRelative.getName()!=null &&!indexItemRelative.getName().equals("")){
            eventExhibitionQueryWrapper.like("s.name",indexItemRelative.getName());
        }
        eventExhibitionQueryWrapper.notExists("SELECT 1 FROM jv_index_item_relative c WHERE c.index_item_id = '"+indexItemRelative.getIndexItemId()+"' and c.content_id = s.event_id and c.type = 6  ");
        eventExhibitionIPage = this.baseMapper.getNoRelativeEventExhibitionPage(eventExhibitionIPage,eventExhibitionQueryWrapper);

        return eventExhibitionIPage;
    }

    @Override
    public IPage<EventExhibition> getIndexRelativeEventExhibitionPage(Integer currentPage, Integer size, IndexItemRelative indexItemRelative) {
        IPage<EventExhibition> eventExhibitionIPage = new Page<>(currentPage, size);
        if(indexItemRelative.getIndexItemId() == null|| indexItemRelative.getIndexItemId().equals("")){
            return eventExhibitionIPage;
        }
        QueryWrapper<EventExhibition> eventExhibitionQueryWrapper = new QueryWrapper<>();
        // 有效关联
        eventExhibitionQueryWrapper.isNotNull("s.id");
        // 板块id
        eventExhibitionQueryWrapper.eq("s.index_item_id",indexItemRelative.getIndexItemId());
        // 名称
        if(indexItemRelative.getName()!=null &&!indexItemRelative.getName().equals("")){
            eventExhibitionQueryWrapper.like("b.name",indexItemRelative.getName());
        }
        eventExhibitionQueryWrapper.eq("b.status",1);
        eventExhibitionIPage = this.baseMapper.getIndexRelativeEventExhibitionPage(eventExhibitionIPage,eventExhibitionQueryWrapper);

        return eventExhibitionIPage;
    }
}
